﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.segments.field;

namespace ConsoleApplication1.segments
{
    class PID
    {
        public StringBuilder set_ID_PID { get; set; }
        public StringBuilder patient_id { get; set; }
        private PN Patient_name;
        private PN Mother_name;
        public StringBuilder date { get; set; }
        private StringBuilder birthday;
        private char sex;
        private AD patient_address;
        private StringBuilder country_code;
        private StringBuilder home_number;
        private StringBuilder mob_number;
        private StringBuilder SSN;
        private CE nationality;
        public bool required { get; set; }

        public PN patient_name { get { return Patient_name; } set { Patient_name = value; } }
        public PN mother_name { get { return Mother_name; } set { Mother_name = value; } }
        public StringBuilder Birthday { get { return birthday; } set { birthday = value; } }
        public char Sex { get { return sex; } set { sex = value; } }
        public AD Patient_address { get { return patient_address; } set { patient_address = value; } }
        public StringBuilder Country_code { get { return country_code; } set { country_code = value; } }
        public StringBuilder Home_number { get { return home_number; } set { home_number = value; } }
        public StringBuilder Mob_number { get { return mob_number; } set { mob_number = value; } }
        public StringBuilder Ssn { get { return SSN; } set { SSN = value; } }
        public CE Nationality { get { return nationality; } set { nationality = value; } }

        public PID()
        {
            Patient_name = new PN();
            Mother_name = new PN();
            patient_address = new AD();
            nationality = new CE();
            set_ID_PID = new StringBuilder();
            patient_id = new StringBuilder();
            birthday = new StringBuilder();
            country_code = new StringBuilder();
            home_number = new StringBuilder();
            mob_number = new StringBuilder();
            Ssn = new StringBuilder();
            date = new StringBuilder();
        }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public int fill(string source, int temp)
        {
            int i,x=0;
            for (i = temp; i < source.Length; i++)
               {
                   if (source[i] == ' ')
                       break;
                   else if (check(source, i) != null)
                   {
                       set_ID_PID.Capacity = set_ID_PID.Length;
                       set_ID_PID.Capacity = set_ID_PID.Length;
                       patient_id.Capacity = patient_id.Length;
                       birthday.Capacity = birthday.Length;
                       country_code.Capacity = country_code.Length;
                       home_number.Capacity = home_number.Length;
                       mob_number.Capacity = mob_number.Length;
                       Ssn.Capacity = Ssn.Length;
                       date.Capacity = date.Length;
                       return i - 1;
                   }
                   else if (source[i] == '|')
                   {
                       if (x == 28)
                       {
                           set_ID_PID.Capacity = set_ID_PID.Length;
                           patient_id.Capacity = patient_id.Length;
                           birthday.Capacity = birthday.Length;
                           country_code.Capacity = country_code.Length;
                           home_number.Capacity = home_number.Length;
                           mob_number.Capacity = mob_number.Length;
                           Ssn.Capacity = Ssn.Length;
                           date.Capacity = date.Length;
                           return i;
                       }

                       else
                       {
                           x++;
                           continue;
                       }
                   }
                   else
                   {
                       if (x == 1)  // passing prameter of set id - PID
                       {
                           if (source[i] != '|')
                               set_ID_PID.Insert(set_ID_PID.Length, source[i]);
                       }
                       else if (x == 2) //passing prameter of patient id
                       {
                           if (source[i] != '|')
                               patient_id.Insert(patient_id.Length, source[i]);
                           //else
                           //{
                           //    x += 3;
                           //    patient_id.Capacity = patient_id.Length;
                           //    break;
                           //}
                       }
                       else if (x >= 3 && x < 5)
                       {
                           while (true)
                           {
                               if (source[i] == '|')
                               {
                                   x++;
                                   break;
                               }
                               i++;
                           }
                       }
                       else if (x == 5) // passing patient name
                       {
                           if (source[i] != '|')
                           {
                               i = patient_name.fill(source, i);
                           }
                       }

                       else if (x == 6) // passing mother name
                       {
                           if (source[i] != '|')
                           {
                               i = mother_name.fill(source, i);
                           }
                       }

                       else if (x == 7) // passing date
                       {
                           if (source[i] != '|')
                               date.Insert(date.Length, source[i]);
                           //else
                           //{
                           //    x++;
                           //    date.Capacity = date.Length;
                           //    break;
                           //}
                       }
                       else if (x == 8) // passing sex  prameter
                       {
                           if (source[i] != '|')
                               sex = source[i];

                           //else
                           //{
                           //    x += 3;
                           //    break;
                           //}
                       }
                       else if (x >= 8 && x < 11)
                       {
                           while (true)
                           {
                               if (source[++i] == '|')
                               {
                                   x++;
                                   break;
                               }
                           }
                       }
                       else if (x == 11)// passing patient address prameter
                       {
                           if (source[i] != '|')
                           {
                               i = patient_address.fill(source, i);
                           }
                       }

                       else if (x == 12) // passing country code
                       {
                           if (source[i] != '|')
                               country_code.Insert(country_code.Length, source[i]);
                           //else
                           //{
                           //    x++;
                           //    break;
                           //}
                       }
                       else if (x == 13) // passing home number
                       {
                           if (source[i] != '|')
                               home_number.Insert(home_number.Length, source[i]);
                           //else
                           //{
                           //    x++;
                           //    break;
                           //}
                       }
                       else if (x == 14) // passing mobile number
                       {
                           if (source[i] != '|')
                               mob_number.Insert(mob_number.Length, source[i]);
                           //else
                           //{
                           //    x += 5;
                           //    break;
                           //}
                       }
                       else if (x >= 15 && x < 19)
                       {
                           while (true)
                           {
                               if (source[i] == '|')
                               {
                                   x++;
                                   break;
                               }
                               i++;
                           }
                       }
                       else if (x == 19)
                       {
                           if (source[i] != '|')
                               Ssn.Insert(Ssn.Length, source[i]);
                           //else
                           //{
                           //    x += 9;
                           //    break;
                           //}
                       }
                       else if (x >= 20 && x < 28)
                       {
                           while (true)
                           {
                               if (source[i] == '|')
                               {
                                   x++;
                                   break;
                               }
                               i++;
                           }
                       }
                       else if (x == 28)
                           if (source[i] != '|')
                           {
                               i = Nationality.fill(source, i);
                               set_ID_PID.Capacity = set_ID_PID.Length;
                               patient_id.Capacity = patient_id.Length;
                               birthday.Capacity = birthday.Length;
                               country_code.Capacity = country_code.Length;
                               home_number.Capacity = home_number.Length;
                               mob_number.Capacity = mob_number.Length;
                               Ssn.Capacity = Ssn.Length;
                               date.Capacity = date.Length;
                               return i;
                           }
                       //else
                       //    return i;

                   }
            }
            
            return i;
        }

        public string tostring()
        {
            string pid_string = "PID";
            
            if (set_ID_PID.Length !=0 )
                pid_string += "|" + set_ID_PID;
            if (patient_id.Length != 0)
                pid_string += "|" + patient_id;
            if (Patient_name != null)
                pid_string += "|" + Patient_name.tostring();
            if (Mother_name != null)
                pid_string += "|" + Mother_name.tostring();
            if (date.Length != 0)
                pid_string += "|" + date;
            if (birthday.Length != 0)
                pid_string += "|" + birthday;
            if (sex != null)
                pid_string += "|" + sex;
            if (patient_address != null)
                pid_string += "|" + patient_address.tostring();
            if (country_code.Length != 0)
                pid_string += "|" + country_code;
            if (home_number.Length != 0)
                pid_string += "|" + home_number;
            if (mob_number.Length != 0)
                pid_string += "|" + mob_number;
            if (SSN.Length != 0)
                pid_string += "|" + SSN;
            if (nationality != null)
                pid_string += "|" + nationality.ToString();
           
            return pid_string;

        }
    }
}
